package com.zzsy.shoppingpro.type.framgent;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.base.BaseFragment;
import com.zzsy.shoppingpro.basebean.BaseBean;
import com.zzsy.shoppingpro.http.BaseCallback;
import com.zzsy.shoppingpro.http.RetrofitHelper;
import com.zzsy.shoppingpro.server.MainService;
import com.zzsy.shoppingpro.type.adapter.TypeLeftAdapter;
import com.zzsy.shoppingpro.type.adapter.TypeRightAdapter;
import com.zzsy.shoppingpro.type.bean.TypeBean;


import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/8/20 0020.
 */

public class ListFragment extends BaseFragment {
    private ListView lv_left;
    private RecyclerView rv_right;


    private boolean isFirst = true;
    private TypeLeftAdapter leftAdapter;

    private List<TypeBean> result;

    @Override
    public View initView() {

        View view = View.inflate(context, R.layout.fragment_list,null);
        initUI(view);
        return view;
    }

    /**
     * 初始化数据
     * */
    private void initUI(View view) {
        lv_left = view.findViewById(R.id.lv_left);
        rv_right =  view.findViewById(R.id.rv_right);
    }

    /**
     * 网络请求数据
     * */
    @Override
    public void initData() {
        getDataFromNet(0);
    }

    /**
     * 请求网络数据
     * */
    private void getDataFromNet(int position) {
        if(position == 0){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType0Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {


                            setData(typeBeanBaseBean);

                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 1){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType1Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                         setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 2){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType2Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                          setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 3){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType3Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 4){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType4Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 5){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType5Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 6){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType6Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 7){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType7Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 8){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType8Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 9){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType9Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else if(position == 10){
            RetrofitHelper.create(MainService.class,Config.BASE_URL_JSON)
                    .getType10Data()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseCallback<BaseBean<List<TypeBean.DataBean>>>() {
                        @Override
                        public void success(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
                            setData(typeBeanBaseBean);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }


    }

    private void setData(BaseBean<List<TypeBean.DataBean>> typeBeanBaseBean) {
        if (isFirst) {
            if(leftAdapter == null){
                leftAdapter = new TypeLeftAdapter(context);
            }
            lv_left.setAdapter(leftAdapter);
        }


        initListener(leftAdapter);
        //解析右边数据
        TypeRightAdapter rightAdapter = new TypeRightAdapter(context, typeBeanBaseBean.getData());
        rv_right.setAdapter(rightAdapter);

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 3;
                } else {
                    return 1;
                }
            }
        });
        rv_right.setLayoutManager(manager);
    }

    private void initListener(TypeLeftAdapter leftAdapter) {
        lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                leftAdapter.changeSelected(position);
                if(position != 0){
                    isFirst = false;
                }
                getDataFromNet(position);
                leftAdapter.notifyDataSetChanged();
            }
        });
        //选中监听
        lv_left.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                leftAdapter.changeSelected(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

}
